package com.khalil.kkadmin.web;

import com.khalil.kkadmin.common.exception.BusinessException;
import com.khalil.kkadmin.common.exception.ExceptionEnum;
import com.khalil.kkadmin.common.web.RespResult;
import com.khalil.kkadmin.infrastructure.web.security.WebContext;
import com.khalil.kkadmin.system.menu.SysMenu;
import com.khalil.kkadmin.system.menu.SysMenuService;
import com.khalil.kkadmin.system.user.SysUserManager;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    private final HttpServletRequest request;
    private final SysUserManager sysUserManager;
    private final SysMenuService sysMenuService;

    public UserController(HttpServletRequest request, SysUserManager sysUserManager, SysMenuService sysMenuService) {
        this.request = request;
        this.sysUserManager = sysUserManager;
        this.sysMenuService = sysMenuService;
    }

    @PostMapping("login")
    public RespResult<String> doLogin(@RequestParam String username, @RequestParam String password, @RequestParam String verCode) {
        // 验证验证码
        if (!verCode.equalsIgnoreCase((String) request.getSession().getAttribute("captcha"))) {
            throw new BusinessException(ExceptionEnum.USER_ERROR_CAPTCHA);
        }

        if (sysUserManager.checkPassword(username, password)) {
            WebContext.login(username);
            return RespResult.ok(username);
        } else {
            throw new BusinessException(ExceptionEnum.USER_PASSWORD_ERROR);
        }
    }

    @GetMapping("captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 设置请求头为输出图片类型
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(90, 28, 4);
        // 设置字体
        specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 20));  // 有默认字体，可以不用设置
        // 设置类型，纯数字、纯字母、字母数字混合
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);

        // 验证码存入session
        request.getSession().setAttribute("captcha", specCaptcha.text().toLowerCase());

        // 输出图片流
        specCaptcha.out(response.getOutputStream());
    }

    @GetMapping("logout")
    public void logout() {
        WebContext.logout();
    }

    @GetMapping("info")
    public RespResult<Object> getUserInfo() {
        return RespResult.ok(WebContext.getUserInfo());
    }

    @PostMapping("changePassword")
    public RespResult<Void> changePassword(String username, String oldPassword, String newPassword) {
        sysUserManager.changePassword(username, oldPassword, newPassword);
        return RespResult.ok(null);
    }

    @GetMapping("menus")
    public RespResult<List<SysMenu>> getMenus() {
        String username = WebContext.getUsername();
        List<SysMenu> sysMenuList = sysMenuService.getUserMenus(username);
        return RespResult.ok(sysMenuList);
    }
}
